// pages/personal/coupons/my-coupons.js

// pages/coupons/coupons.js
var app = getApp()
Page({
    data: {
        /** 
            * 页面配置 
            */
        // winWidth: 0,
        // winHeight: 0,
        // tab切换  
        // currentTab: 0,
        tabs: [
            // { label: "全部", status: 0 },
            { label: "未使用", status: 1 },
            { label: "已使用", status: 2 },
            { label: "已过期", status: 3 },            
        ],
        current: 0
    },
    onLoad: function () {
        var that = this;

        /** 
         * 获取系统信息 
         */
        wx.getSystemInfo({

            success: function (res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }
        });
        this.loadCoupons()
        // this.compute()
    },
    clickTab(evt) {
        this.setData({
            current: evt.currentTarget.dataset.current,
        })
        this.loadCoupons()
    },
    condition(e) {
        var index = e.currentTarget.dataset.index;
        var val = this.data.tabs[this.data.current].list[index].isShow
        this.setData({
            [`tabs[${this.data.current}].list[${index}].isShow`]: !val
        })
    },
    popMask(e) {
        var index = e.currentTarget.dataset.index;
        var useTips = this.data.tabs[this.data.current].list[index].content
        wx.showModal({
            title: '使用说明',
            content: useTips.replace(/<\/?[^>]*>/g, ""),
            showCancel: false,
        })
    },
    loadCoupons(){
        const { current, tabs } = this.data
        let { page = 1, total = 2, list = [], loading = false } = tabs[current]
        if (page > total || loading) {
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            [`tabs[${current}].loading`]: true,
        })
        app.http.get("goods/self-coupons", {
            data: {
                status: tabs[current].status,
            }
        }).then(res => {
            page++
            total = res.data.total
            list.push(...res.data.info)
            loading = false
            this.setData({
                [`tabs[${current}]`]: Object.assign(tabs[current], { page, total, list, loading })
            })
            wx.hideLoading()
            wx.stopPullDownRefresh()
        })
    },

    // // 计算优惠券是否过期
    // compute(){

    // },

    /** 
       * 滑动切换tab 
       */
    // bindChange: function (e) {
    //     var that = this;
    //     that.setData({ currentTab: e.detail.current });
    // },
    /** 
     * 点击tab切换 
     */
    // swichNav: function (e) {

    //     var that = this;

    //     if (this.data.currentTab === e.target.dataset.current) {
    //         return false;
    //     } else {
    //         that.setData({
    //             currentTab: e.target.dataset.current
    //         })
    //     }
    // }
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        const { current, tabs } = this.data
        this.setData({
            [`tabs[${current}]`]: Object.assign(tabs[current], { page: 1, total: 2, list: [] })
        })
        this.loadCoupons()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.loadCoupons()
    }
})  